package course

import (
	"context"
	"github.com/jinzhu/copier"
	"xialou_platform/app/respx"
	"xialou_platform/app/tools"
	"xialou_platform/services/agent/rpc/agentClient"

	"xialou_platform/services/agent/api/internal/svc"
	"xialou_platform/services/agent/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CourseDetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCourseDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) CourseDetailLogic {
	return CourseDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CourseDetailLogic) CourseDetail(req *types.CourseIdReq) (resp *types.CourseDetailResp, err error) {
	course, err := l.svcCtx.AgentRpc.CourseById(l.ctx, &agentClient.IdBody{Id: respx.IDParse(req.Id).GetInt64()})
	if err != nil {
		return nil, err
	}

	resp = &types.CourseDetailResp{}
	copier.Copy(&resp, course)
	resp.Id = respx.IDFrom(course.Id).ToString()
	resp.CreateTime = tools.TimeUnixShowLayoutString(course.CreateTime)
	return resp, nil
}
